//index.js
//获取应用实例
const common = require('../../utils/util.js')
const app = getApp()

Page({
    data: {
        nowDate: '',
        isLogin: false,
        isBind: false,
        userInfo: null,
        shopInfo: null,
        imgSwiper: [],
        shopVideo: [],
        cardGoods: [],
        faceGoods: [],
        classify: [],
        classifyGoods: [],
        popImg: null,
        homePop: true,
        relayInfo: null
    },

    onLoad: function (options) {
        if (wx.getStorageSync('token')) {
            this.getUserInfo()
            this.setData({
                isLogin: true
            })
            if (wx.getStorageSync('storeId') != 1 || wx.getStorageSync('parentId') != 0) {
                this.setData({
                    isBind: true
                })
                wx.showTabBar()
            } else {
                wx.hideTabBar()
            }
        } else {
            wx.hideTabBar()
        }
        this.setData({
            nowDate: new Date().getTime()
        })

        // if (options.storeId) {
        //     wx.setStorageSync('storeId', options.storeId)
        //     if (wx.getStorageSync('token')) {
        //         let data = {
        //             store_id: options.storeId
        //         }
        //         common.post("Store/change_user_store", data).then(function (res) {}).catch(function (e) {})
        //     }
        // }

        if (options.userShare) {
            wx.setStorageSync('userShare', options.userShare)
            if (wx.getStorageSync('token')) {
                let data = {
                    belong_refer: options.userShare
                }
                common.post('User/set_belong_refer', data).then(function (res) {}).catch(function (e) {})
            }
        }

        this.getOnOff()
        this.getPopImg()
        this.getRelayInfo()
        this.getShopInfo()
        this.getImgSwiper()
        this.getShopVideo()
        this.getCardGoods()
        this.getFaceGoods()
        this.getClassify()
    },

    onHide: function () {

    },

    onShow: function () {

    },

    onPullDownRefresh: function () {
        this.setData({
            nowDate: '',
            isLogin: false,
            shopInfo: null,
            imgSwiper: [],
            shopVideo: [],
            cardGoods: [],
            faceGoods: [],
            classify: [],
            classifyGoods: []
        })
        common.refresh()
        setTimeout(function () {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    onReachBottom: function () {

    },

    onShareAppMessage: function (res) {
        let userId = wx.getStorageSync('userId')
        let storeId = wx.getStorageSync('storeId')
        if (userId) {
            return {
                title: this.data.relayInfo.share_txt,
                path: '/pages/index/index?userShare=' + userId,
                imageUrl: this.data.relayInfo.share_images
            }
        }
    },

    getUserInfo: function () {
        const that = this
        common.post('User/user_info', {}).then(function (res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                userInfo: res
            })
        }).catch(function (e) {})
    },

    getOnOff: function () {
        const that = this
        common.post('Home/index', {
            activity: 785
        }).then(function (res) {
            if (res.code == 204) {
                return
            }
            if (res.show_video == app.globalData.isShow) {
                that.setData({
                    isShow: true
                })
            } else {
                that.setData({
                    isShow: false
                })
            }
        }).catch(function (e) {})
    },

    getPopImg: function () {
        const that = this
        let data = {
            activity: 785,
            images_id: 49
        }
        common.post('Images/get_img', data).then(function (res) {
            that.setData({
                popImg: res[0]
            })
        }).catch(function (e) {})
    },

    getRelayInfo: function () {
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId')
        }
        common.post('Store/share_images', data).then(function (res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                relayInfo: res
            })
        }).catch(function (e) {})
    },

    getShopInfo: function () {
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId')
        }
        common.post('Store/get_store_info', data).then(function (res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                shopInfo: res
            })
            wx.setNavigationBarTitle({
                title: res.store_name
            })
        }).catch(function (e) {})
    },

    getImgSwiper: function () {
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId'),
            is_online: 1,
            page: 0,
            limit: 99
        }
        common.post('Slideshow/slideshow_list', data).then(function (res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                imgSwiper: res
            })
        }).catch(function (e) {})
    },

    getShopVideo: function () {
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId'),
            page: 0,
            limit: 6
        }
        common.post('Video/video_list', data).then(function (res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    shopVideo: res
                })
            }
        }).catch(function (e) {})
    },

    getCardGoods: function () {
        const that = this
        let data = {
            activity: 785,
            is_sale: '1',
            is_interval: '1',
            page: 0,
            limit: 10
        }
        common.post('Goodslist/goods_list', data).then(function (res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                cardGoods: res
            })
        }).catch(function (e) {})
    },

    getFaceGoods: function () {
        const that = this
        let data = {
            activity: 785,
            is_sale: '1',
            is_goods: '11',
            page: 0,
            limit: 10
        }
        common.post('Goodslist/goods_list', data).then(function (res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                faceGoods: res
            })
        }).catch(function (e) {})
    },

    getClassify: function () {
        const that = this
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId'),
            type: 1,
            show_type: 4,
            page: 0,
            limit: 999
        }
        common.post('Goodsclass/store_class_list', data).then(function (res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                classify: res
            })
            res.forEach((e, i) => {
                that.getClassifyGoods(e.class_id, i)
            })
        }).catch(function (e) {})
    },

    getClassifyGoods: function (id, index) {
        const that = this
        let classifyGoods = this.data.classifyGoods
        let data = {
            activity: 785,
            store_id: wx.getStorageSync('storeId'),
            class_id: id,
            is_sale: '1',
            page: 0,
            limit: 10
        }
        common.post('Goods/goods_list', data).then(function (res) {
            if (res.code == 204) {
                classifyGoods[index] = []
                that.setData({
                    classifyGoods: classifyGoods
                })
                return
            }
            classifyGoods[index] = res
            that.setData({
                classifyGoods: classifyGoods
            })
        }).catch(function (e) {})
    },

    closePop: function () {
        this.setData({
            homePop: false
        })
    },

    goMyShop: function () {
        wx.showLoading({
            title: '请稍后'
        })
        const that = this
        let data = {
            store_id: this.data.userInfo.real_store_id
        }
        common.post("Store/change_user_store", data).then(function (res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            }
            wx.setStorageSync('storeId', that.data.userInfo.real_store_id)
            wx.reLaunch({
                url: '/pages/index/index'
            })
        }).catch(function (e) {})
    }
})